package com.zhenyi.websocket.core.ws;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class WSUtils {

	public static final int PORT = 8989;
	public static final String WS_PATH = "/ws";
	EventLoopGroup bossGroup = new NioEventLoopGroup();
	EventLoopGroup workerGroup = new NioEventLoopGroup();

	public void stop() {
		workerGroup.shutdownGracefully();
		bossGroup.shutdownGracefully();
	}

	public void start() {

		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
					.childHandler(new WebSocketChannelInitializer());
			// serverBootstrap.childHandler(new WebSocketChannelInitializer2());
			// serverBootstrap.handler(new LoggingHandler(LogLevel.ERROR));

			// ChannelPoolMap<InetSocketAddress, SimpleChannelPool> poolMap =
			// new AbstractChannelPoolMap<InetSocketAddress,
			// SimpleChannelPool>() {
			// @Override
			// protected SimpleChannelPool newPool(InetSocketAddress key) {
			// System.out.println("key:" + key.getHostString());
			// return new FixedChannelPool(strap.remoteAddress(key), new
			// NettyChannelPoolHandler(), 2);
			// }
			// };
			System.out.println("start ws: " + PORT + " path: " + WS_PATH);
			ChannelFuture channelFuture = serverBootstrap.bind(PORT).sync();
			channelFuture.channel().closeFuture().sync();

		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}

}
